<?php
namespace app\admin\controller;
use \think\Controller;
use \Firebase\JWT\JWT;
use app\user\controller\User;
use think\Db;
use think\Request;
use think\facade\Session;
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST,GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
class Authinfo extends Controller
{

	/**
	 * @var \think\Request Request实例
	 */
	protected $request;

	/**
	 * 构造方法
	 * @param Request $request Request对象
	 * @access public
	 */
	public function __construct(Request $request)
	{
//		exit('12');

		$access_token= $request->param('access_token');

		$uid=$request->param('uid');

		$token=$this->accesstoken($access_token,$uid);



		if($token['info']['status']==100001){

			echo '{status:10001}';
			exit();
//        $this->redirect('Logintoken/yanzheng',['canshu'=>$token['info']['info']]);
		}else{
			$group_id=Db::name('Auth_group_access')->where('uid',$uid)->value('group_id');

			if($group_id>=1){

				define('UID',   $token['info']['uid']);
				define('NICKNAME',$token['info']['nickname']);
				$controller = request()->controller();
				$action = request()->action();
				if($group_id>1){
					$kongzhi=$controller.'/'.$action;
					$apiauth=get_apiquanxian($kongzhi,$uid,$group_id);
//					dump($kongzhi);exit;
					if($apiauth==1){

					}else{
						echo '{status:10003}';
						exit();
					}
				}
			}else{

				echo '{status:10002}';
				exit();
			}

//        if(UID!=1){
//
//     		$auth = new Auth();
//     		if(!$auth->check('admin/'.$controller . '/' . $action, UID)){
//
//
//     		}#$this->redirect('Logintoken/noauth');
//        }

		}
		//$this->redirect('Index/yanzheng',['canshu'=>$access_token]);

	}



	public function accesstoken($access_token,$uid)
	{

		$key1=HB_JWT_TOKEN;
		$key=$key1.$uid;
		JWT::$leeway = 60; // $leeway in secondsdum
		$decoded = JWT::decode($access_token, $key, array('HS256'));


		$shujus['status']=1;
		$shujus['info']=object_array($decoded);
		return $shujus;
	}
}
